﻿var imgLoading=new Image();
imgLoading.src='/images/imgLoading.gif';
var loadingImg=new Image();
loadingImg.src='/images/loading.gif';
var loadFailImg=new Image();
loadFailImg.src='/images/icoErr.gif';
var loadingTxt='<p style="text-align:center;color:#999;padding:8px;"><img src="'+loadingImg.src+'" /> 数据加载中...</p>';
var loadFailTxt='<p style="text-align:center;color:red;font-size:12px; padding:8px;"><img src="'+loadFailImg.src+'" /> 数据加载失败，请<a href="javascript:location=location.href.toString()">刷新</a>重试...</p>';
var loadedFailTxt='<p style="text-align:center;color:red;font-size:12px; padding:8px;">数据加载失败，请$link$重试...</p>';
var dataIsEmpty='<p style="text-align:center;color:gray;line-height:21px;">暂时没有数据</p>';

//浏览器检测
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
try{
if(window.ActiveXObject)
    Sys.ie=ua.match(/msie ([\d.]+)/)[1];
else if(ua.match(/firefox\/([\d.]+)/))
    Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1];
else if(ua.match(/chrome\/([\d.]+)/))
    Sys.chrome=ua.match(/chrome\/([\d.]+)/)[1];
else if(window.opera)
    Sys.opera=ua.match(/opera.([\d.]+)/)[1];
else if(window.openDatabase){
    Sys.safari=ua.match(/version\/([\d.]+)/)[1];
    }
}catch(e){}

function $$(id){
    return document.getElementById(id);
}

function tab(tit,cnt){
	var tags=$$(tit).getElementsByTagName('div'); 
	var objCnt=$$(cnt);
	var len=tags.length-1; 
	var flag=0;//修改默认值
	objCnt.className="ajaxLoad1";
    objCnt.innerHTML='';
	for(i=1;i<len;i++){	
		tags[i].value = i;
		tags[i].onclick=function(){
		    objCnt.innerHTML='';
		    objCnt.className="ajaxLoad1";             			
			tags[flag].className='title0';
			flag=this.value;	
			tags[flag].className='title1';
		};
//		tags[i].onmouseover=function(){
//		        clearTimeout(timer);
//		}
//	    tags[i].onmouseout=function(){
//	            timer=setTimeout("settime()",t);
//	    }
	};
	tags[flag].className='title1';
}

function tabHover(tit,cnt,titTag,cntTag,time){
    var objTitle=$$(tit),
        objCnt=$$(cnt);
    var titTags=objTitle.getElementsByTagName(titTag),
        cntTags=objCnt.getElementsByTagName(cntTag),
        len=titTags.length;
	var flag=0;//修改默认值
	if(len<1) return;
	if(!time) time=150;
	for(i=0;i<len;i++){	
		titTags[i].value = i;
		titTags[i].onmouseover=function(){
		    var val=this.value;
		    if(!this.timer) window.clearTimeout(this.timer);    //if(!this.timer)
			this.timer=setTimeout(function(){
			    titTags[flag].className='';
			    cntTags[flag].style.display='none';			    
			    flag=val;	
			    cntTags[flag].style.display='block';
			    titTags[flag].className='over';
			},time);
		};
		titTags[i].onmouseout=function(){
		    clearTimeout(this.timer);
		};
	};
	titTags[flag].className='over';
	cntTags[flag].style.display='block';
}

function tabClick(tit,cnt,titTag,cntTag){
    var objTitle=typeof(tit)=='string'?$$(tit):tit,
        objCnt=typeof(cnt)=='string'?$$(cnt):cnt;
    var titTags=objTitle.getElementsByTagName(titTag),
        cntTags=objCnt.getElementsByTagName(cntTag),
        len=titTags.length;
	var flag=0;//修改默认值
	if(len<1) return;

	for(i=0;i<len;i++){	
		titTags[i].value = i;
		titTags[i].onclick=function(){    
		    titTags[flag].className='';
		    //cntTags[flag].style.display='none';			    
		    $(cntTags[flag]).slideUp('fast');
		    flag=this.value;	
		    //cntTags[flag].style.display='block';
		    titTags[flag].className='over';
		    $(cntTags[flag]).slideDown('fast');
		};
	};
	titTags[flag].className='over';
	cntTags[flag].style.display='block';
}

function tabList(targetId,hover,cntTag)
{    
    var time=150;
    var obj=$$(targetId);
    if(!obj) return;
    cntTag=cntTag?cntTag:'div';
    var titleTags=obj.getElementsByTagName('h3')[0].getElementsByTagName('a'),
        cntTags=obj.getElementsByTagName(cntTag);
    var len=titleTags.length,
        flag=0;//修改默认值
	for(i=0;i<len;i++){	
		titleTags[i].value = i;
		if(hover){
		    titleTags[i].onmouseover=function(){
		        var val=this.value;
		        if(this.timer) window.clearTimeout(this.timer); 
		        this.timer=setTimeout(function(){
		            titleTags[flag].className='';
			        cntTags[flag].style.display='none';
			        flag=val;
			        cntTags[flag].style.display='block';
			        titleTags[flag].className='over';
			    },time);	    
		    }
		    titleTags[i].onmouseout=function(){
		        clearTimeout(this.timer);
		    };
		}else{
		    titleTags[i].onclick=function(){
		        titleTags[flag].className='';
			    cntTags[flag].style.display='none';
			    flag=this.value;
			    cntTags[flag].style.display='block';
			    this.className='over';
		    };
		}
	};
	titleTags[flag].className='over';
	cntTags[flag].style.display='block';
}

function setPage(currentPageIndex, pageSize, recordcount, callbackFunName,callbackFunArgs)
{
    if(recordcount<=pageSize) return '';
    var pageCount, pagecode = '';
    pageCount=Math.ceil(recordcount/pageSize);

    pagecode='<div class="paging">';
    if (currentPageIndex > 1)
        pagecode+='<a href="javascript:void(0)" onclick="'+callbackFunName+'(1,'+recordcount+callbackFunArgs+')">首 页</a>'+
                '<a href="javascript:void(0)" onclick="'+callbackFunName+'('+(currentPageIndex - 1)+','+recordcount+callbackFunArgs+')">上一页</a>';
    else
        pagecode+='<span>首 页</span><span>上一页</span>';
        
    if (currentPageIndex < pageCount)
        pagecode+='<a href="javascript:void(0)" onclick="'+callbackFunName+'('+(currentPageIndex + 1)+','+recordcount+callbackFunArgs+')">下一页</a><a href="javascript:void(0)" onclick="'+callbackFunName+'('+pageCount+','+recordcount+callbackFunArgs+')">尾 页</a>';
    else
        pagecode+='<span>下一页</span><span>尾 页</span>';

    pagecode+='<em>每页<b>'+pageSize+'</b>条 共<b>'+recordcount+'</b>条 当前第 <b>'+currentPageIndex+'</b> 页 共 <b>'+pageCount+'</b> 页</em></div>';
    return pagecode;
}

//正则表达式验证（是否为中文）
function isCn(str){
    if(str=='') return false;  
    var pattern = /^([\u4e00-\u9fa5])*$/;
    if(pattern.test(str))
        return true;
    else
        return false;
}

//正则表达式验证（是否为中文字母或数字）
function isCnNumberLetter(str){   
    if(isCn(str)||isNumberAndLetter(str)) return true;
    return false;
}

//正则表达式（邮箱地址）
function isEmail(str){
    if(str=='') return false;
    var pattern = /^(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*$/;   
    if(pattern.test(str))    
        return true;      
    else
        return false;
}

function isNumberAndLetter(str){
    if(str=='') return false;
    var pattern=/^([0-9a-z_])*$/;
    if(pattern.test(str))
        return true;
    else
        return false;
}

function isURI(uri){
    if(uri.indexOf('.')<2) return false;
    var strRegex = "^((https|http)?://)?" 
          + "(([0-9a-zA-Z_-]+\.)*" 
          + "([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\." 
          + "[a-zA-Z]{2,6})" 
          + "(:[0-9]{1,4})?" 
          + "((/?)|" 
          + "(/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
    var regex=new RegExp(strRegex); 
    return regex.test(uri);
}

function selectAllCheckBox(obj,chkName)
{
    var tags=document.getElementsByName(chkName);
    var len=tags.length;
    var isChecked=obj.checked;
    var objNext=obj.nextSibling;
    for(var i=0;i<len;i++){
        tags[i].checked=isChecked;
    }
    
    if(isChecked)
        objNext.innerHTML='取消';
    else
        objNext.innerHTML='全选';
}
function InvertSelectCheckBox(obj,chkName)
{
    var tags=document.getElementsByName(chkName);
    var len=tags.length;
    for(var i=0;i<len;i++){
        tags[i].checked=!tags[i].checked;
    }
}
//去掉数组中的重复值
function unique(data){
    var a = {};
    for (var i=0; i<data.length; i++)
    {
        var v = data[i];
        if (typeof(a[v]) == 'undefined') a[v] = 1;
    };
    data.length=0;
    for (var i in a) data[data.length] = i;
    return data;
}
//兼容IE与FireFox,chrome等Click事件
function overrideClick(obj)
{
    if(document.all){
        obj.click();
    }else{
        var evt = document.createEvent("MouseEvents");
        evt.initEvent("click", true, true);
        obj.dispatchEvent(evt);
    }
}
function getFirstChildNextSibling(obj)
{
    return getNextSibling(getFirstChild(obj));
}
function getFirstChild(obj)
{
    var objChild=obj.firstChild;
    while(objChild.nodeType==3){
        objChild=objChild.nextSibling;
    }
    return objChild;
}
function getLastChild(obj){
    var objChild=obj.lastChild;
    while(objChild.nodeType==3){
        objChild=objChild.previousSibling;
    }
    return objChild;
}
function getNextSibling(obj) {
    var objNext = obj.nextSibling;
    while (objNext && objNext.nodeType == 3) {
        objNext = objNext.nextSibling;
    }
    return objNext;
}
function getPreviousSibling(obj)
{
    var objPrevious=obj.previousSibling;
    if(objPrevious.nodeType!=3) return objPrevious;
    var loop=true;
    while(loop){
        objPrevious=objPrevious.nextSibling;
        if(objPrevious.nodeType!=3) loop=false;
    }
    return objPrevious;
}
//两个参数，一个是cookie的名子，一个是值,一个COOKIE保存天数
function setCookie(name,value,days){
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + days*24*60*60*1000);
    document.cookie = name + "="+ escape(value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;//decodeURIComponent(arr[2])
}

function getCookies(name,index)
{
    var position=-1;
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr==null) return null;
    var cookieArr=arr[2].split('&');
    for(var i=0;i<cookieArr.length;i++)
    {
        if(cookieArr[i].indexOf(index)!=-1)
        {
            position=i;
            break;
        }
    }
    if(position==-1) return null;    
    return decodeURIComponent(cookieArr[position].substring(cookieArr[position].indexOf('=')+1));
}

function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function AutoResizeImage(maxWidth,maxHeight,objImg)
{
    if (typeof(objImg)=='string'){ objImg = document.getElementById(objImg); }
    var img = new Image();
    img.onload = function(){
        var hRatio;
        var wRatio;
        var Ratio = 1;
        var w = img.width;
        var h = img.height;
        wRatio = maxWidth / w;
        hRatio = maxHeight / h;
        wRatio = wRatio>1?1:wRatio;
        hRatio = hRatio>1?1:hRatio;
        if (maxWidth ==0 && maxHeight==0){
            Ratio = 1;
        }else if (maxWidth==0){
            if (hRatio<1) Ratio = hRatio;
        }else if (maxHeight==0){
            if (wRatio<1) Ratio = wRatio;
        }else{
            Ratio = (wRatio<=hRatio?wRatio:hRatio);
        }
        if (Ratio<1)
        {
            w = w * Ratio;
            h = h * Ratio;
        }
        objImg.height = h;
        objImg.width = w;        
        this.onload = function(){}
    }
    img.src = objImg.src;
}
function ReloadImg(objImg,imgpath)
{
    var img=new Image();
    img.src='/images/imgLoadFail.gif';
    objImg.src=img.src;
    objImg.onclick=function(){
        objImg.src=imgpath;
    }
}

function getArrayRandomItems(arr, num) {
    var tmpArr = new Array();
	for (var i in arr) {
		tmpArr.push(arr[i]);
	}
    var randomArr = new Array();
	for (var i = 0; i<num; i++) {
        if (tmpArr.length>0) {
            var arrIndex = Math.floor(Math.random()*tmpArr.length);
            randomArr[i] = tmpArr[arrIndex];
            tmpArr.splice(arrIndex, 1);
		}else{
            break;
		}
	}
	return randomArr;
}

//字符串中全角，半角空格删除
String.prototype.clearSpace=function()
{  
    return this.replace(/\s+|　/g,"");
}
String.prototype.Trim=function() 
{ 
    return this.replace(/(^\s*)|(　*)|(\s*$)/g, ""); 
}   
String.prototype.Ltrim=function() 
{ 
    return this.replace(/(^\s*)/g, ""); 
}
String.prototype.Rtrim=function() 
{ 
    return this.replace(/(\s*$)/g, ""); 
}
String.prototype.left=function(cutlength,isAddEllipsis)
{
     if(this==null||this.length<1) return this; 
     var substr=''; 
     var strlen = 0;//字符串长度，一个汉字长度为2 
     for (var i=0; i<this.length; i++) {  
        var c = this.charCodeAt(i);          
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f))   //单字节加1
             strlen++;
        else
             strlen+=2;  
        if(strlen>cutlength && i<=this.length-1) break;  
        substr+=this.charAt(i);
     } 
     if(isAddEllipsis&&strlen>cutlength) substr+='...'; 
     return substr;
}

String.prototype.decodeURI = function () {
     if (this.length < 1) return '';
     var str = decodeURIComponent(this).replace(/[+]/gi, ' ');
     str = str.replace(/&#8211;/g, '-');
     str = str.replace(/&acute;/g, "'");
     str = str.replace(/&quot;/g, "\\");
     return str;
 }
String.prototype.toTxt=function()
{
    var str= this.replace(/&#8211;/g,'-');
    str = str.replace(/&acute;/g, "'");
    str = str.replace(/&quot;/g, "\\");
    return str
}

function openWriteSuggestWindow()
{
    var objWindow=ZHAO.Dialog;
    objWindow.style='width:480px;';
    objWindow.innerHTML='<div class="tiptitle">说说我使用的感受</div>\
     <div id="dialogBody" class="tipbody"><div style="padding:8px;"><strong>意见&建议：</strong><br />\
     <textarea id="pop_sug_cnt" rows="8" cols="80" style="width:440px;"></textarea><div></div></div>\
     <div id="tipbtn" class="tipbtn"><input id="tipsubmit" type="button" class="btn" value="确 定" onclick="submitSuggestForm()" /> \
     <input id="tipcancel" class="btnGray" type="button" value="取 消" onclick="ZHAO.Dialog.Close()" /></div></div>';
     objWindow.Open();
}
function submitSuggestForm()
{
    var objCnt=document.getElementById('pop_sug_cnt'),
        objTipBody=document.getElementById('dialogBody'),
        objNext=objCnt.nextSibling,
        cnt=objCnt.value;
    if(cnt.length<5||cnt.length>1000){
        objNext.innerHTML='<span class="err">内容不能小于5个字符，大于1000个字符</span>';
        return false;
    }
    objTipBody.innerHTML='<p align="center"><img src="'+loadingImg.src+'" /> 数据提交中...</p>';
    $.ajax({
        type:'post',
        data:'contents='+cnt,
        url:'/ajax/suggest/add.ashx',
        success:function(msg){
            if(msg=='ok')
                objTipBody.innerHTML='<p align="center">感谢你的宝贵意见，我们会尽快处理。<br /><br /><a href="javascript:void(0)" onclick="ZHAO.Dialog.Close()">关 闭</a></p>';
            else
                top.location.href='/suggest.aspx?flag=fail';
        }
    });
    return;
}

function floatSuggest(id,_top,_left)
{    
    var isInit=document.getElementById(id);
    if(!isInit)
    {
        var panel='<div style="float:left;width:157px;height:200px;"></div><div class="outBtn" onclick="openWriteSuggestWindow()">说说我使用的感受</div>';
        var objDiv=document.createElement('div');
        objDiv.id=id;
        objDiv.innerHTML=panel;
        document.body.appendChild(objDiv);
    }
	var obj=id.charAt?document.getElementById(id):id, db=document.body, dd=document.documentElement;
	db.style.height=dd.style.height='100%';
	obj.style.top=_top?_top+'px':0;obj.style.left=_left+"px";
	obj.style.position='absolute';
	setInterval(function (){obj.style.top=parseInt(obj.style.top)+(Math.max(db.scrollTop,dd.scrollTop)+_top-parseInt(obj.style.top))*0.1+'px';},10+parseInt(Math.random()*20));
}

function getRadioGroupVal(name){
    var arr=document.getElementsByName(name);
    var len=arr.length,
        result=-1;
    for(var i=0;i<len;i++){
        if(arr[i].checked){
            result=arr[i].value;
            break;
        }
    }
    return result;
}

//获取单行文本框光标位置 
function getPositionForInput(obj){ 
    var curPos = 0; 
    if (document.selection) { // IE 
        obj.focus(); 
        var objSel = document.selection.createRange(); 
        objSel.moveStart('character', -obj.value.length); 
        curPos = objSel.text.length; 
    }else if(obj.selectionStart || obj.selectionStart == '0'){// Firefox
        curPos = obj.selectionStart;
    }     
    return (curPos); 
} 
//获取多行文本框光标位置
function getPositionForTextArea(obj) { 
    var curPos = 0; 
    if(document.selection) {// IE
        obj.focus(); 
        var objSel = document.selection.createRange(); 
        var objSel2 = objSel.duplicate(); 
        objSel2.moveToElementText(obj); 
        var curPos = -1; 
        while(objSel2.inRange(Sel)){ 
            objSel2.moveStart('character'); 
            curPos++; 
        } 
    }else if(obj.selectionStart || obj.selectionStart == '0'){// Firefox
        curPos = obj.selectionStart; 
    } 
    return (curPos); 
}
//设置光标位置函数 
function setCursorPosition(obj, pos){ 
    if(obj.setSelectionRange){  
        obj.setSelectionRange(pos,pos); 
    } 
    else if (obj.createTextRange) { 
        var range = obj.createTextRange(); 
        range.collapse(true); 
        range.moveEnd('character', pos); 
        range.moveStart('character', pos); 
        range.select(); 
    } 
}